
import TaskStep from '@/components/taskStep.vue';

import apiMixin from '@/mixin/apiMixin';
import taskMixin from '@/mixin/taskMixin';

import keyup from '@/mixin/keyup';

import uilts from '@/utils/index';
const diffcultyLevelObj = {
    1: '初级',
    2: '中级',
    3: '高级'
}
export default {
    mixins: [apiMixin,taskMixin,keyup],
    data() {
        return {
            loading: false,
            handleShow:false,
            baseLink: {
                taskTitle: '',
                unitPrice: 0,
                acceptId: '',
                taskId: '',
                sortName: '',
                // 仲裁状态
                atStatus: 0,
                // 任务难度
                diffcultyLevel: '初级',
                taskPushTime: '',
                drawTaskTime: '',
                achieveTaskTime: '',
                arbitrateTaskTime: '',
                customClearTime: '',
                dealer: []
            },
            acceptUserInfo: {},
            pushUserInfo: {},
            taskSteps: [],
            recordList: [],
            globalTimer: null,
            commitTimeObj: {
                acceptClose: 0,
                publishClose: 0,
                createTime: 0,
                acceptCloseTime: 0,
                publishCloseTime: 0,
            },
            // 仲裁弹窗状态
            atPopStatus: -1,
            atPopText: '',
            dispostBtnLoad: false
        }
    },
    mounted() {
        this.getTaskIdList();
        this.init();
    },
    computed: {
    },
    methods: {
        openFile(url) {
            open(url,"_blank");
        },
        init() {
            this.getArbitrationDetailApi();
        },
        initPar() {
            clearInterval(this.globalTimer);
            this.atPopStatus = -1;
            this.atPopText = '';
            this.commitTimeObj = {
                acceptClose: 0,
                publishClose: 0,
                createTime: 0,
            };
        },
        getArbitrationDetailApi() {
            let id = this.$route.query.id;
            let _self = this;
            clearInterval(this.globalTimer);
            let soltObj = {
                // 基础信息
                'link': (res) => {
                    let { arbitration_info,task_info,
                        accept_user_info,push_user_info, 
                        dealer
                    } = res;
                    _self.baseLink.taskTitle = task_info.title;
                    _self.baseLink.unitPrice =  _self.priceFormat(task_info.amount_actual);
                    _self.baseLink.acceptId = task_info.id;
                    _self.baseLink.taskId = task_info.task_id;
                    _self.baseLink.sortName = task_info.first_name + '-' + task_info.seconde_name;
                    _self.baseLink.atStatus = arbitration_info.status;
                    _self.acceptUserInfo = accept_user_info;
                    _self.pushUserInfo = push_user_info;
                    _self.baseLink.diffcultyLevel = diffcultyLevelObj[arbitration_info.diffculty_level];
                    _self.baseLink.taskPushTime = _self.countTrialDate(task_info.task_publish_time * 1000);
                    _self.baseLink.drawTaskTime = _self.countTrialDate(task_info.task_accept_time * 1000);
                    _self.baseLink.achieveTaskTime = _self.countTrialDate(task_info.task_end_time * 1000);
                    _self.baseLink.arbitrateTaskTime = _self.countTrialDate(arbitration_info.report_time * 1000);
                    _self.baseLink.customClearTime = arbitration_info.close_time ? _self.countTrialDate(arbitration_info.close_time * 1000) : '-';
                    _self.baseLink.dealer = dealer;
                    _self.taskSteps = task_info.custom_steps;
                },
                // 仲裁记录
                'record': (res) => {
                    let { log } = res;
                    for (let i=0;i<log.length;i++) {
                        if (log[i].accept_close_time > 0 || log[i].publish_close_time > 0) {
                            _self.commitTimeObj.acceptClose = log[i].accept_close_time + log[i].time_created;
                            _self.commitTimeObj.publishClose = log[i].publish_close_time + log[i].time_created;
                            _self.commitTimeObj.createTime = log[i].time_created;
                        }
                    }
                    _self.globalTimer = setInterval(()=>{
                        if (_self.commitTimeObj.acceptClose <=0 && _self.commitTimeObj.publishClose <= 0) {
                            clearInterval(_self.globalTimer);
                        }
                        if (_self.commitTimeObj.acceptClose > 0) {
                            _self.$set( 
                                _self.commitTimeObj,
                                'acceptCloseTime',
                                _self.countTimeDown(_self.commitTimeObj.acceptClose,'accept')
                            );
                            //_self.commitTimeObj.acceptCloseTime = _self.countTimeDown(_self.commitTimeObj.acceptClose,'accept');
                        }
                        if (_self.commitTimeObj.publishClose > 0) {
                            _self.$set( 
                                _self.commitTimeObj,
                                'publishCloseTime',
                                _self.countTimeDown(_self.commitTimeObj.publishClose,'publish')
                            );
                            //_self.commitTimeObj.publishCloseTime = _self.countTimeDown(_self.commitTimeObj.publishClose,'publish');
                        }
                    },998)
                    _self.recordList = log;
                }
            };
            this.comResquest(this.request.getArbitrationDetail,(res)=>{
                soltObj['link'](res);
                soltObj['record'](res);
            },{
                id,
                load: true
            });   
        },
        countTimeDown(seconds,type) {
            let curDateMS = parseInt(new Date().getTime() /1000) ;
            let residueDate = seconds - curDateMS;
            if (residueDate <= 0) {
                if (type === 'accept') {
                    this.commitTimeObj.acceptClose = 0;
                }else{
                    this.commitTimeObj.publishClose = 0;
                }
                return '上传时间结束';
            }
            const formatSeconds = value => {
                var secondTime = parseInt(value);// 秒
                var minuteTime = 0;// 分
                var hourTime = 0;// 小时
                if(secondTime > 60) {//如果秒数大于60，将秒数转换成整数
                    //获取分钟，除以60取整数，得到整数分钟
                    minuteTime = parseInt(secondTime / 60);
                    //获取秒数，秒数取佘，得到整数秒数
                    secondTime = parseInt(secondTime % 60);
                    //如果分钟大于60，将分钟转换成小时
                    if(minuteTime > 60) {
                        //获取小时，获取分钟除以60，得到整数小时
                        hourTime = parseInt(minuteTime / 60);
                        //获取小时后取佘的分，获取分钟除以60取佘的分
                        minuteTime = parseInt(minuteTime % 60);
                    }
                }
                var result = "" + parseInt(secondTime) + "秒";
                if(minuteTime > 0) {
                    result = "" + parseInt(minuteTime) + "分" + result;
                }
                if(hourTime > 0) {
                    result = "" + parseInt(hourTime) + "小时" + result;
                }
                return result;
            }
            return  formatSeconds(residueDate)
        },
        changeAtPopText(atIndex) {
            let str = '';
            switch (atIndex) {
                case 1:
                    str =
                        "经过检查，目前已核实接单人做的任务确实未达标，现将仲裁判定派单人胜利！";
                    break;
                case 2:
                    str =
                        "经过检查，目前已核实派单人申请的仲裁确实有误，现将仲裁判定接单人胜利！";
                    break;
                case 3:
                    str =
                        "经过检查，目前无法判定接单人任务是否未完成，现需要派单人上传复审材料以便客服审核，请注意上传能够证明的材料。";
                    break;
                case 4:
                    str =
                        "经过检查，目前无法判定接单人任务是否完成，现需要接单人上传复审材料以便客服审核，请注意上传能够证明的材料。";
                    break;
                case 5:
                    str =
                        "经过检查，目前无法判定仲裁，现需要双方上传各自的复审材料以便审核。";
                    break;
                case 6:
                    str =
                        "经过检查，目前发现双方均无异常，任务将重新释放出去。";
                    break;
                case 7:
                    str =
                        "经过检查，目前发现双方均无异常，任务将被清算退款至派单人。";
                    break;
                default:
                    str = "";
            };
            this.atPopText = str;
        },
        postDisposeArbitrationApi() {
            if (this.atPopStatus == -1) {
                this.$message.warning("未选择处理结果");
                return;
            }
            if (!this.atPopText) {
                this.$message.warning("未填写处理原因");
                return;
            }
            let id = this.$route.query.id;
            let _self = this;
            if (this.dispostBtnLoad) {
                return;
            }
            this.comResquest(this.request.postDisposeArbitration,(res)=>{
                _self.$notify.info({
                    title: '审批结果',
                    message: res.info
                });
                _self.handleShow = false;
                this.initPar();
                this.init();
            },{
                ids: [id],
                status: this.atPopStatus,
                remark: this.atPopText,
                load: "dispostBtnLoad"
            }); 
        },
        handlePopShow() {
            this.handleShow = !this.handleShow;
        },
        taskOperateLockApi(type) {
            let id = this.$route.query.id;
            let _self = this;
            this.comResquest(this.request.taskOperateLock,(res)=>{
                console.log(res);
                let { code,info } = res;
                if (Number(code) == 1) {
                    _self.$notify.error({title: '错误',message: info});
                }
                _self.handlePopShow();
            },{
                id: id,
                // unlock lock
                action: type,
                table: "task_accept_arbitration"
            }); 
        },
        getTaskIdList() {
            this.getIdListApi(this.request.getArbitrationList);
        },     
        switchPage() {
            return new Promise((resolve,reject)=>{
                let _self = this;
                this.comResquest(this.request.getArbitrationList,(res)=>{
                    resolve(res);
                },{
                    load: true,
                    page: this.listPage,
                    id_only: 1,
                    pagesize: 10
                })
            })
        },
        hanldeQuiteNav(type) {
            let id = this.$route.query.id;
            let _self = this;
            this.comQuiteNav(type,id,(changeId)=>{
                _self.$router.push({ query: { id: changeId,listPage: this.listPage } });
                _self.init();
            });
        }
    },
    components:{
        TaskStep
    },
}